#include "database.h"
#include "log.h"

DataBase *DataBase::base = NULL;


DataBase::DataBase()
{
    bool fileExist = QFile::exists(Config::baseFileName);
        if(!fileExist){
            Log::add("[Database]Base file not found!!!");
        }else{
            _base = QSqlDatabase::addDatabase("QSQLITE",Config::baseFileName);
            _base.setDatabaseName(Config::baseFileName);
            bool isOpened = _base.open();
            if(!isOpened){
                Log::add("[Database]Can't open data base");
            }else{
                Log::add("[Datebase] Create and open");
                Log::add(QString("[Database] Load %1 tabels").arg(_base.tables().count()));
            }
        }
}

QString DataBase::getName(int id)
{
    QString nick;
    QSqlQuery query(_base);
    QString strQuery = "SELECT NickName  FROM NickNamesTable WHERE id = %1 ;";
    strQuery = strQuery.arg(id);
    query.prepare(strQuery);
    query.exec();
    query.next();
    if (!query.isValid()){
        Log::add("[Database]Invalid user id");
    }
    nick = query.value(0).toString();
    query.finish();
    return nick;
}

int DataBase::getLevel(int id)
{
    int level;
    QSqlQuery query(_base);
    QString strQuery = "SELECT level FROM Users WHERE id= %1 ;";
    strQuery = strQuery.arg(id);
    query.prepare(strQuery);
    query.exec();
    query.next();
    if (!query.isValid()){
        Log::add("[Database]Invalid user id");
    }
    level = query.value(0).toInt();
    query.finish();
    return level;
}

int DataBase::isAutherazation(QString login,QString password)
{
    QSqlQuery query(_base);
    QString strQuery = "SELECT id,Login,Password FROM AuthorizationData WHERE Login = '%1';";
    strQuery = strQuery.arg(login);
    query.prepare(strQuery);
    query.exec();
    query.next();
    if (!query.isValid()){
        Log::add("[Database]Invalid user login");
        query.finish();
        return 0;
    }
    else{
        if(query.value(2)!= password){
            Log::add(QString("[Database]Invalid password for login %1").arg(query.value(0).toString()));
            query.finish();
            return 0;
        }else{
            int id;
            Log::add(QString("[Database]User with login %1 find and entered").arg(query.value(0).toString()));
            id = query.value(0).toInt();
            query.finish();
            return id;
        }
    }
}

QString DataBase::getStatus(int id)
{
    QString status;
    QSqlQuery query(_base);
    QString strQuery = "SELECT Status FROM Users WHERE id = %1 ;";
    strQuery = strQuery.arg(id);
    query.prepare(strQuery);
    query.exec();
    query.next();
    if (!query.isValid()){
        Log::add("[Database]Invalid user id");
    }
    status = query.value(0).toString();
    query.finish();
    return status;
}

int DataBase::getWin(int id)
{
    int win;
    QSqlQuery query(_base);
    QString strQuery = "SELECT winGames FROM Users WHERE id = %1;";
    strQuery = strQuery.arg(id);
    query.prepare(strQuery);
    query.exec();
    query.next();
    if (!query.isValid()){
        Log::add("[Database]Invalid user id");
    }
    win = query.value(0).toInt();
    query.finish();
    return win;
}

int DataBase::getLose(int id)
{
    int lose;
    QSqlQuery query(_base);
    QString strQuery = "SELECT loseGames FROM Users WHERE id = %1 ;";
    strQuery = strQuery.arg(id);
    query.prepare(strQuery);
    query.exec();
    query.next();
    if (!query.isValid()){
        Log::add("[Database]Invalid user id");
    }
    lose = query.value(0).toInt();
    query.finish();
    return lose;
}
